<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\Plugin\DrainageLink\Task;

use EasyWeChat\Kernel\Exceptions\InvalidConfigException;
use GuzzleHttp\Exception\GuzzleException;
use Hyperf\Contract\StdoutLoggerInterface;
use Hyperf\Crontab\Annotation\Crontab;
use Hyperf\Di\Annotation\Inject;
use MoChat\Plugin\DrainageLink\Queue\LinkClickStatisticQueue;

/**
 * 每小时执行一次
 * @Crontab(name="LinkClickStatistic", rule="0 * * * *", callback="execute", singleton=true, memo="企业微信-引流链接点击统计")
 */
class LinkClickStatisticTask
{
    /**
     * @Inject
     * @var LinkClickStatisticQueue
     */
    protected $linkClickStatisticQueue;

    /**
     * @Inject
     * @var StdoutLoggerInterface
     */
    private $logger;

    public function execute(): void
    {
        $this->logger->info('引流链接点击统计开始');
        # # 异步处理
        try {
            $this->linkClickStatisticQueue->handle();
        } catch (InvalidConfigException|GuzzleException $e) {
            $this->logger->error('引流链接点击统计异步调用失败' . $e->getMessage());
        }
        $this->logger->info('引流链接点击统计完成');
    }
}
